package com.lsiyun.sync_file.app;

import com.lsiyun.sync_file.Utils;
import com.lsiyun.sync_file.protocol.SyncFile;
import com.lsiyun.sync_file.protocol.SyncMessage;
import com.lsiyun.sync_file.protocol.SyncProtocol;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static final int PORT = 8888;
    public static final String SYNC_DIR_SERVER = "F:/sync/server/";
    private static final File SYNC_DIR = new File(SYNC_DIR_SERVER);


    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(PORT);

        while (true) {
            Socket socket = serverSocket.accept();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        while (true) {
                            ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
                            SyncMessage syncMessage = (SyncMessage) inputStream.readObject();
                            if (syncMessage.getType() == SyncProtocol.C2S_CLOSE) {
                                socket.close();
                                break;
                            }
                            receive(socket, syncMessage);
                        }
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }

                    System.out.println("同步线程结束");
                }
            }).start();
        }
    }

    private static void receive(Socket socket, SyncMessage syncMessage) throws Exception {
        if (syncMessage.getType() == SyncProtocol.C2S_DELETE_FILE) {
            ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
            SyncFile clientFile = syncMessage.getSyncFile();
            new File(SYNC_DIR, clientFile.getPath()).delete();
        } else if (syncMessage.getType() == SyncProtocol.C2S_ADD_FILE) {
            SyncFile clientFile = syncMessage.getSyncFile();
            if (clientFile.isFile()) {
                var file = new File(SYNC_DIR, clientFile.getPath());
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                Utils.transData(socket.getInputStream(), fileOutputStream);
                fileOutputStream.close();
                file.setLastModified(clientFile.getUpdateTime());


            } else if (clientFile.isDir()) {
                new File(SYNC_DIR, clientFile.getPath()).mkdirs();
            }
        } else if (syncMessage.getType() == SyncProtocol.C2S_GET_FILE) {
            SyncFile clientFile = syncMessage.getSyncFile();
            FileInputStream fileOutputStream = new FileInputStream(new File(SYNC_DIR, clientFile.getPath()));
            Utils.transData(fileOutputStream, socket.getOutputStream());
            fileOutputStream.close();
        } else if (syncMessage.getType() == SyncProtocol.C2S_GET_FILES_INFO) {
            SyncFile syncFile = new SyncFile(SYNC_DIR_SERVER, "");
            ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
            outputStream.writeObject(syncFile);
        } else if (syncMessage.getType() == SyncProtocol.C2S_UPDATE_TIME) {
            SyncFile clientFile = syncMessage.getSyncFile();
            var file = new File(SYNC_DIR, clientFile.getPath());
            file.setLastModified(clientFile.getUpdateTime());
        } else if (syncMessage.getType() == SyncProtocol.C2S_CLOSE) {
            socket.close();
        }
    }
}
